﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Controllers;
using System.Linq.Expressions;

namespace BPM
{
	/// <summary>
	/// Action Definition
	/// </summary>
	public class AD
	{
		/// <summary>
		/// 所有的Action定义都存储在这个属性中
		/// </summary>
		public static readonly AD Singleton = new AD();
		private AD() { }

		public readonly LogonControllerActions Logon = new LogonControllerActions();
		public readonly InternalDefaultViewControllerActions InternalDefaultView = new InternalDefaultViewControllerActions();
		public readonly AuthorityControllerActions Authority = new AuthorityControllerActions();
		public readonly RegisterControllerActions Register = new RegisterControllerActions();
		public readonly CategoryControllerActions Category = new CategoryControllerActions();
		public readonly PurchaseOrderControllerActions PurchaseOrder = new PurchaseOrderControllerActions();
		public readonly MaterialManagementControllerActions MaterialManagement = new MaterialManagementControllerActions();
		public readonly SearchControllerActions Search = new SearchControllerActions();
		public readonly MaterialInDeliveryControllerActions MaterialInDelivery = new MaterialInDeliveryControllerActions();
		public readonly MaterialDeliveryControllerActions MaterialDelivery = new MaterialDeliveryControllerActions();
		public readonly AssembleControllerActions Assemble = new AssembleControllerActions();
		public readonly ErrorControllerActions Error = new ErrorControllerActions();

		public static ADPair Read(Expression<Func<AD, string>> actionExp)
		{
			var actionProperty = (actionExp.Body as MemberExpression);
			if (actionProperty == null) throw new ArgumentNullException("action");

			var controllerProperty = (actionProperty.Expression as MemberExpression);
			if (controllerProperty == null) throw new ArgumentNullException("action");

			var controller = typeof(AD).GetField(controllerProperty.Member.Name).GetValue(AD.Singleton);
			var action = controller.GetType().GetField(actionProperty.Member.Name).GetValue(controller);

			return new ADPair()
			{
				Controller = controller.ToString(),
				Action = action.ToString()
			};
		}
	}

	public class ADPair
	{
		public string Controller;
		public string Action;

		public string Format(string rear = null)
		{
			return this.Controller + "/" + this.Action + "/" + rear;
		}
	}

	public abstract class ControllerDefinition
	{
		protected abstract string Name { get; }

		public override string ToString()
		{
			return this.Name;
		}
	}

	public abstract class ControllerDefinition<TController> : ControllerDefinition
	{
		protected ControllerDefinition()
		{
			this._name = typeof(TController).Name.Replace("Controller", string.Empty);
		}

		private string _name;

		public static implicit operator string(ControllerDefinition<TController> value)
		{
			return value._name;
		}

		protected override string Name
		{
			get
			{
				return this._name;
			}
		}
	}
}
